<!-- 登录 -->
<template>
	<view class="container">
		<!-- 背景图 -->
		<view class="x-c sign-bg">
			<image class="logo-bg"  :src="$IMG_URL + '/imgs/logo_bg.png'" mode="aspectFill"></image>
		</view>
		<!-- titleview -->
		<view class="head-box">
			<u-navbar :is-back="false" :background="navBackground"></u-navbar>
		</view>
		<view class="wrapper">
			<!-- logo -->
			<view class="x-c">
				<image class="logo" src="/static/images/logo.png" mode="widthFix"></image>
				<!-- <image class="logo" :src="sysInfo.logo" mode="widthFix"></image> -->
			</view>
			<!-- 登录tab -->
			<view class="tab-box x-f">
				<view class="tab-item x-c" @tap="onLoginWay(0)">
					<text class="tab-title">手机登录</text>
					<view class="line-box" v-show="loginWay === 0"><text class="triangle"></text></view>
				</view>
				<view class="tab-item x-c" @tap="onLoginWay(1)">
					<text class="tab-title">密码登录</text>
					<view class="line-box" password v-show="loginWay === 1"><text class="triangle"></text></view>
				</view>
			</view>
			<!-- 表单 -->
			<view class="login-box y-f" v-show="loginWay === 0">
				<view class="input-item x-c"><input class="inp" v-model="userPhone" type="number" placeholder="请输入手机号" placeholder-class="pl" /></view>
				<view class="input-item x-c">
					<input class="inp" v-model="code.value" type="number" placeholder="请输入验证码" placeholder-class="pl" />
					<button class="u-reset-button code-btn" :disabled="code.status" @tap="getCode">{{ code.text }}</button>
				</view>
			</view>
			<view class="login-box y-f" v-show="loginWay === 1">
				<view class="input-item x-c"><input class="inp" v-model="userPhone" type="number" placeholder="请输入账号" placeholder-class="pl" /></view>
				<view class="input-item x-c"><input class="inp" password v-model="userPassword" type="text" placeholder="请输入密码" placeholder-class="pl" /></view>
			</view>
			<!-- 登录按钮 -->
			<view class="x-c y-f">
				<button class="coco-btn login-btn mb30" @tap="toLogin">登录</button>
				<view class="x-bc tip-box ">
					<button class="u-reset-button tip-btn" @tap="jump('/pages/public/register')">立即注册</button>
					<view class="" v-show="loginWay === 1">
						<button class="u-reset-button tip-btn" @tap="jump('/pages/public/forgot')">忘记密码</button>
					</view>
				</view>
				<view class="tip-box" @tap="onTcp">
					<label class="x-f">
						<radio class="tcp-radio coco-radio" :class="{ checked: isTcp }" :checked="isTcp"></radio>
						<view class="">
							我已阅读并遵守 
							《<text class="tcp" @tap="jump('/pages/public/richtext', { id: 6 })">用户协议</text>》
							与
							《<text class="tcp" @tap="jump('/pages/public/richtext', { id: 2 })">隐私协议</text>》
						</view>
					</label>
				</view>
			</view>
			<!--<view class="third-party y-f" v-if="shop.is_online">-->
				<!--&lt;!&ndash; #ifdef H5 &ndash;&gt;-->
				<!--<button class="u-reset-button wx-logo-box y-f" @tap="wxLogin">-->
					<!--<image class="auto-login"  :src="$IMG_URL + '/imgs/auto_login_wx.png'" ></image>-->
					<!--<view class="">微信一键登录</view>-->
				<!--</button>-->
				<!--&lt;!&ndash; #endif &ndash;&gt;-->
				<!--&lt;!&ndash; #ifdef MP-WEIXIN &ndash;&gt;-->
				<!--<button class="u-reset-button wx-logo-box y-f" @tap="wxLogin">-->
					<!--<image class="auto-login"  :src="$IMG_URL + '/imgs/auto_login_wx.png'" ></image>-->
					<!--<view class="">微信一键登录</view>-->
				<!--</button>-->
				<!--&lt;!&ndash; #endif &ndash;&gt;-->
				<!--&lt;!&ndash; #ifdef APP-PLUS &ndash;&gt;-->
				<!--<button class="u-reset-button wx-logo-box y-f" @tap="wxLogin">-->
					<!--<image class="auto-login"  :src="$IMG_URL + '/imgs/auto_login_wx.png'" ></image>-->
					<!--<view class="">微信一键登录</view>-->
				<!--</button>-->
				<!--&lt;!&ndash; #endif &ndash;&gt;-->
			<!--</view>-->
		</view>
	</view>
</template>

<script>
import wechat from '@/shopro/wechat/wechat';
import { mapMutations, mapActions, mapState } from 'vuex';
import store from '@/shopro/store';
export default {
	data() {
		return {
			navBackground:{
				backgroundColor:'rgba(255,255,255,0)'
			},
			code: {
				text: '获取验证码',
				status: false,
				value: ''
			},
			loginWay: 0,
			userPhone: '',
			userPassword: '',
			isTcp: false, //协议
			sysInfo: uni.getStorageSync('sysInfo')
		};
	},
	computed: {
		...mapState({
			shop: ({ shopro }) => shopro.config, // 版本信息
		}),
	},
	onLoad() {
		console.log("shop",this.shop);
		if (this.$Route.query.token) {
			this.setTokenAndBack(this.$Route.query.token);
		}
	},
	onShow() {
		let token = uni.getStorageSync('token');
		if(token){
			uni.reLaunch({
				url:"/pages/index/index"
			})
		}
	},
	methods: {
		...mapActions(['getUserInfo', 'showAuthModal','init']),
		// #ifdef MP-WEIXIN
		async getuserinfo(e) {
			// var wechat = new Wechat();
			// let token = await wechat.wxMiniProgramLogin(e);
			// store.commit('FORCE_OAUTH', false);
			// this.setTokenAndBack(token);
		},
		// #endif
		async wxLogin() {
			if (!this.isTcp) {
				this.$u.toast('请先同意协议！');
				return false;
			}
			let token = await wechat.login();
			if (token !== undefined) {
				this.getUserInfo(token);
				this.$u.toast("登录成功");
				setTimeout(()=>{
					uni.reLaunch({
						url:"/pages/index/index"
					})
				},2000);
			}
		},
		onLoginWay(flag) {
			this.loginWay = flag;
		},
		onTcp() {
			this.isTcp = !this.isTcp;
		},
		toLogin() {
			if (!this.isTcp) {
				this.$u.toast('请先同意协议！');
				return false;
			}
			let that = this;
			if (that.loginWay === 0) {
				that.$http('user.smsLogin', {
					mobile: that.userPhone,
					code: that.code.value
				}).then(res => {
					if (res.code === 1) {
						// that.closeAuthModal();
						that.getUserInfo(res.data.token);
						that.$u.toast(res.msg);
						setTimeout(()=>{
							uni.reLaunch({
								url:"/pages/index/index"
							})
						},2000);
						// that.jump("/pages/index/index");
						// that.setTokenAndBack(res.data.userinfo.token);
						
					}
				});
			}
			if (that.loginWay === 1) {
				that.$http('user.accountLogin', {
					account: that.userPhone,
					password: that.userPassword
				}).then(res => {
					if (res.code === 1) {
						that.closeAuthModal();
						that.getUserInfo(res.data.token);
						that.$u.toast(res.msg);
						// that.jump("/pages/index/index");
						uni.reLaunch({
							url:"/pages/index/index"
						})
					}
				});
			}
		},
		closeAuthModal() {
			this.showAuth = false;
			this.$store.dispatch('showAuthModal', '');
		},

		// 获取短信
		async getCode() {
			let that = this;
			that.code.status = true;
			let countdown = 60;
			that.$http('common.smsSend', {
				mobile: that.userPhone,
				event: 'mobilelogin'
			}).then(res => {
				if (res.code === 1) {
					that.code.text = countdown + '秒';
					that.code.status = true;
					let timer = setInterval(() => {
						if (countdown > 0) {
							that.code.text = countdown - 1 + '秒';
							countdown--;
						} else {
							clearInterval(timer);
							that.code.text = '获取验证码';
							that.code.status = false;
						}
					}, 1000);
				} else {
					that.code.status = false;
				}
			});
		},
		// 路由跳转
		jump(path, parmas) {
			this.$Router.push({
				path: path,
				query: parmas
			});
		}
	}
};
</script>

<style lang="scss">
	page{
		height: 100%;
	}
	/* x水平排列*/
	.x-f {
		display: flex;
		align-items: center;
	}
	/*x两端且水平居中*/
	.x-bc {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	/*x平分且水平居中*/
	.x-ac {
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	/*x水平靠上对齐*/
	.x-start {
		display: flex;
		align-items: flex-start;
	}
	/*x水平靠下对齐*/
	.x-end {
		display: flex;
		align-items: flex-end;
	}
	/*上下左右居中*/
	.x-c {
		display: flex;
		justify-content: center;
		align-items: center;
	}
	/*y竖直靠左*/
	.y-start {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
	}
	/*y竖直靠右*/
	.y-end {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
	}
	/*y竖直居中*/
	.y-f {
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	// y竖直两端
	.y-b {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}
	/*y竖直两端居中*/
	.y-bc {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
	}
	
.container {
	position: relative;
	width: 100%;
	height: 100%;
	// overflow: hidden;
	background: linear-gradient(to bottom, rgba(215, 232, 255, 1) 0%, rgba(241, 247, 255, 1) 50%, rgba(255, 255, 255, 1) 100%);

	// titleview
	.head-box {
		.cuIcon-back {
			font-size: 50rpx !important;
			font-weight: 500;
		}
	}

	// logo
	.logo {
		width: 150rpx;
		height: 150rpx;
		border-radius: 40rpx;
	}
	.sign-bg{
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		z-index: 1;
		pointer-events: none;
	}
	.logo-bg {
		width: 750rpx;
		height: 480rpx;
	}
}

.wrapper {
	position: relative;
	z-index: 90;
	padding-bottom: 40upx;
	padding-top: 96upx;
	
	.tip-box {
		margin-top: 40rpx;
		width: 630rpx;
		line-height:40rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: rgba(99, 114, 148, .8);
	
		.tcp-radio {
			 
		}
	
		.tcp {
			text-decoration: underline;
		}
	}

	// 登录选项卡
	.tab-box {
		margin: 60rpx auto 30rpx;
		width: 608rpx;

		.tab-item {
			flex: 1;
			height: 80rpx;
			position: relative;

			.tab-title {
				font-size: 32rpx;
				font-weight: bold;
				color: #102044;
			}

			.line-box {
				position: absolute;
				width: 300rpx;
				height: 4rpx;
				background: #9da9bd;
				bottom: 0;
				left: 50%;
				transform: translateX(-50%);

				.triangle {
					position: absolute;
					bottom: 0;
					left: 50%;
					transform: translateX(-50%);
					disply: block;
					width: 0;
					height: 0;
					border-width: 10rpx;
					border-style: solid;
					border-color: transparent transparent #9da9bd transparent;
				}
			}
		}
	}

	// 输入
	.login-box {
		.input-item {
			position: relative;
			height: 108rpx;
			// border-bottom: 1rpx solid rgba(#c1cad9, 0.5);
			width: 608rpx;
			&::after{
				content: ' ';
				position: absolute;
				left: 0;
				right: 0;
				bottom: 0;
				height: 1px;
				background-color: #c1cad9;
				transform: scaleY(.5);
			}
			.inp {
				flex: 1;
				height: 100%;
				font-size: 28rpx;
			}

			.pl {
				color: rgba(99, 114, 148, .8);
			}

			.code-btn {
				background: none;
				font-size: 28rpx;
				color: #102044;
			}
		}
	}

	// 登录
	.login-btn {
		width: 630upx;
		height: 80upx;
		border-radius: 40rpx;
		margin-top: 70upx;
		background: linear-gradient(to right, rgba(78,146,252, 1), rgba(77, 146, 240, 1));
		box-shadow: 0px 7rpx 6rpx 0px rgba(78,146,252, 0.22);
		color: #fff;
		font-size: 30upx;
	}
	

	.tip-box {
		width: 630rpx;

		.tip-btn {
			font-size: 28rpx;
			font-weight: 500;
			text-decoration: underline;
			color: rgba(99, 114, 148, .8);
			background: none;
		}
	}

	// 一键登录按钮
	.wx-logo-box {
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: rgba(99, 114, 148, .8);
		margin-top: 60rpx;
		background: none;
		display: block;
		&:hover {
			background: none;
		}
		.auto-login {
			width: 80rpx;
			height: 80rpx;
			overflow: hidden;
			margin-bottom: 10rpx;
		}
	}
}
</style>
